package com.learn.use.myreconsituteproject.http;

import android.content.Context;
import android.text.TextUtils;

import com.learn.use.myreconsituteproject.util.NetworkUtils;
import com.learn.use.myreconsituteproject.util.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.util.Map;

/**
 * Created by 聪明一只哈 on 2018/9/12.
 * Time: 2018/9/12  16:27
 */
public class OkGoUtils {

    public static void postRequest(Context context,
                                   String url,
                                   Map<String,String> params,
                                   final Request request){

        if (NetworkUtils.isConnected(context)) {
            PostRequest<String> postRequest = OkGo.<String>post(url)
                    .params(params).tag(context);

            postRequest.execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    String body = response.body();
                    if(body!=null&& !TextUtils.isEmpty(body)){
                        request.onSuccess(body);

                    }
                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                    request.onError(response.toString() + "~~~失败" );

                }
            });
        }else {
            ToastUtils.showShortToast(context, "网络链接失败，请检查网络设置");
        }


    }

}
